<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新品有戏商家合作平台</title>
    <script src="__JS__/jquery.js"></script>
    <link href="__CSS__/boot.css" rel="stylesheet">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <script src="__JS__/layer/layer.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <style>
        tr td{
            text-align: center;
        }
    </style>
</head>
<body>
{include file="base/header" /}
<div style="height: 1px"></div>
<table class="layui-table" id="test_table">
    <thead>
    <tr>
        <td>商品名称</td>
        <td>卖出数(个)</td>
        <td>剩余数(个)</td>
        <td>已结算数(个)</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    {foreach $result as $val}
    <tr>
        <td>{$val.pname}</td>
        <td>{$val.sellnumber}</td>
        <td>{$val.number - $val.sellnumber}</td>
        {if isset($val.anumber)}
            <td>{$val.anumber}</td>
        {else/}
            <Td>0</Td>
        {/if}
        <td>
            {if isset($val.anumber)}
                <button type="button" onclick="settlement({$val.id},{$val.sellnumber - $val.anumber})" class="btn btn-primary btn-xs">申请结算</button>
            {else/}
                <button type="button" onclick="settlement({$val.id},{$val.sellnumber})" class="btn btn-primary btn-xs">申请结算</button>
            {/if}
        </td>
    </tr>
    {/foreach}
    </tbody>
</table>
</body>
<script>
    function settlement(inventoryid,sellnumber) {
        if(sellnumber <= 0){
            layer.alert('没有需要结算的订单');
            return;
        }
        $.ajax({
            url:"{:url('business/order/settlement')}",
            data:"inventoryid="+inventoryid+"&sellnumber="+sellnumber,
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.success){
                    layer.alert(res.success,{bnt:['确定','取消']},function () {
                        window.location.reload();
                    },function () {
                        window.location.reload();
                    });
                }else if(res.error){
                    layer.alert(res.error);
                }
            }
        });
    }
</script>
</html>